'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 离开战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../consts/code');
var bossRecordModel = require('../../../models/bossRecordModel');
var PlayerManager = require('../../../libs/playerManager');
var rewardService = require('../../../services/rewardService');
var bossService = require('../../../services/bossService');
var bossRankService = require('../../../services/bossRankService');
var taskService = require('../../../services/taskService');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 退出BOSS战
 * 需要上传的数据:
 * 1.已经拆除的建筑
 * 2.已经打过的经济类的建筑
 * 3.要统计的数据
 */
Handler.prototype.exit = function (msg, session, next) {
    var role_id = session.uid;
    var record_id = msg.record_id;
    var boss_damage = msg.boss_damage || 0;

    if (!record_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var boss_record_model;
    var reward_list;
    var rank;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                bossRecordModel.getRecordByAtkRoleId(role_id, record_id, cb);
            },
            function (model, cb) {
                boss_record_model = model;
                if (!boss_record_model) {
                    return next(null, {
                        code: Code.BOSS_RECORD_NOT_EXIST
                    });
                }

                if (!!boss_record_model.isPass()) {
                    return next(null, {
                        code: Code.BOSS_IS_PASS
                    });
                }

                player_manager.getModel(
                    ['role', 'boss', 'bag', 'build', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb);
            },
            function (cb) {
                var boss_model = player_manager.boss;

                boss_record_model.setBossDamange(boss_damage);
                boss_model.setDamage(boss_damage);

                // 增加副本统计
                taskService.addWorldBossNum(player_manager);
                taskService.addInMorgui(player_manager);

                player_manager.save(cb);
            },
            function (cb) {
                bossRankService.getCurrentRoleBossDamageRank(role_id, cb);
            },
            function (result, cb) {
                rank = result.rank;
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            boss_record_model.save();
            return next(null, {
                code: Code.OK,
                result: {
                    boss_damage: boss_damage,
                    rank: rank,
                    // reward_list: reward_list
                }
            });
        }
    );
};
